package com.ebelter.temperaturegun.adapters;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.models.bluetooth.products.temperature2.Temperature2Manager;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.TgManager;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.utils.TemGunUtil;

import java.util.ArrayList;
import java.util.List;

public class MainHisLvAdapter extends BaseAdapter{

    private List<GetTempertureData.ResultDataBean.DataBean> datas;

    private Context mContext;

    public int unitModel;//0代表°C 1代表°F
    public MainHisLvAdapter(Context mContext) {
        this.mContext = mContext;
        unitModel=TemUtils.getUerUnit();
    }

    public void updateUnit(){
        unitModel=TemUtils.getUerUnit();
        notifyDataSetChanged();
    }

    public List<GetTempertureData.ResultDataBean.DataBean> getDatas() {
        return datas;
    }

    public void addDatas(List<GetTempertureData.ResultDataBean.DataBean> d){
        if (datas==null){
            datas=new ArrayList<>();
        }
        datas.addAll(d);
        notifyDataSetChanged();
    }

    public void addData(int index,GetTempertureData.ResultDataBean.DataBean d){
        if (datas==null){
            datas=new ArrayList<>();
        }
        datas.add(index,d);
        notifyDataSetChanged();
    }

    public void setDatas(List<GetTempertureData.ResultDataBean.DataBean> d){
        datas=d;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas==null?0:datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View cV, ViewGroup parent) {


        Vh vh;
        if (cV==null){
            vh = new Vh();
            cV = View.inflate(mContext, R.layout.mainhislv_item,null);
            vh.m_his_time_date_tv= cV.findViewById(R.id.m_his_time_date_tv);
            vh.m_his_time_tv= cV.findViewById(R.id.m_his_time_tv);
            vh.m_his_display_tv= cV.findViewById(R.id.m_his_display_tv);
            vh.m_his_weizhi_tv= cV.findViewById(R.id.m_his_weizhi_tv);
            vh.m_his_station_tv= cV.findViewById(R.id.m_his_station_tv);
            cV.setTag(vh);
        }else {
            vh= (Vh) cV.getTag();
        }

        GetTempertureData.ResultDataBean.DataBean bean = datas.get(position);

        long currentItemTime= TimeUtils.parseFormatter1Time(bean.testDate).getTime();


        dealDateTv(vh.m_his_time_date_tv,currentItemTime,position);

        ViewUtils.setTextViewStr(vh.m_his_time_tv,TimeUtils.formatTime_shf(mContext,currentItemTime));


        if (unitModel==0){
            //C
            ViewUtils.setTextViewStr(vh.m_his_display_tv, TemGunUtil.getDisTemC(bean.getTemperature())+" °C");
        }else {
            //F
            ViewUtils.setTextViewStr(vh.m_his_display_tv, TemGunUtil.getDisTemF(bean.getTemperature())+" °F");
        }

        //(1:耳温,2:额温)
        if (bean.tempType==1){
            ViewUtils.setTextViewStr(vh.m_his_weizhi_tv,CommonLib.getString(R.string.erwenceliang));
        }else {

            ViewUtils.setTextViewStr(vh.m_his_weizhi_tv, CommonLib.getString(R.string.ewenceliang));
        }

        ViewUtils.setTextViewStr(vh.m_his_station_tv, TemUtils.getResultDesc(bean.temperature));

        return cV;
    }

    private void dealDateTv(TextView m_his_time_date_tv, long currentItemTime, int currentItemPosition) {

        String currentDate = TimeUtils.formatDate_nyr(mContext,currentItemTime);

        ViewUtils.setTextViewStr(m_his_time_date_tv,currentDate);
        if (currentItemPosition==0){
            ViewUtils.displayView(m_his_time_date_tv);
        }else if (currentItemPosition>0){

            long lastItemTime = TimeUtils.parseFormatter1Time(datas.get(currentItemPosition-1).testDate).getTime();
            String lastDate = TimeUtils.formatDate_nyr(mContext,lastItemTime);

            if (TextUtils.equals(currentDate,lastDate)){
                ViewUtils.goneView(m_his_time_date_tv);
            }else {
                ViewUtils.displayView(m_his_time_date_tv);
            }


        }


    }

    public void notifyUnit() {
        unitModel=TemUtils.getUerUnit();
        notifyDataSetChanged();
    }


    class Vh{
        TextView m_his_time_date_tv;//日期
        TextView m_his_time_tv;//时间
        TextView m_his_display_tv;//显示温度
        TextView m_his_weizhi_tv;//位置
        TextView m_his_station_tv;//描述
    }
}
